/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.entities;

import ru.ssau.lib.PresentableAsStringIntegerPair;
import java.io.Serializable;
import java.util.Date;
import org.jdom.Element;
import ru.ssau.lib.StringIntegerPair;
import ru.ssau.lib.XMLizable;
import ru.ssau.utils.XMLHelper;

public class User implements Serializable, PresentableAsStringIntegerPair, XMLizable {

    private int id;
    private String name;
    private Date registrationDate;
    private String info;
    private AuthorizationInfo authorizationInfo;

    public User(int id, String name, Date registrationDate,
            String info, AuthorizationInfo authorizationInfo) {
        this.id = id;
        this.name = name;
        this.registrationDate = registrationDate;
        this.info = info;
        this.authorizationInfo = authorizationInfo;
    }

    public Element toXMLElement() {
        Element root = new Element("User");
        XMLHelper.addElement(root, "id", id);
        XMLHelper.addElement(root, "name", name);
        XMLHelper.addElement(root, "registrationDate", registrationDate);
        XMLHelper.addElement(root, "info", info);
        XMLHelper.addElement(root, authorizationInfo.toXMLElement());
        return root;
    }

    public void loadFromXMLElement(Element data) throws NumberFormatException {
        Element temp = null;
        if ((temp = data.getChild("id")) != null) {
            setId(Integer.valueOf(temp.getValue()));
        }
        if ((temp = data.getChild("name")) != null) {
            setName(temp.getValue());
        }
        if ((temp = data.getChild("registrationDate")) != null) {
            setRegistrationDate(new Date(Long.valueOf(temp.getValue())));
        }
        if ((temp = data.getChild("info")) != null) {
            setInfo(temp.getValue());
        }
        AuthorizationInfo authInfo = new AuthorizationInfo();
        if ((temp = data.getChild("AuthorizationInfo")) != null) {
            authInfo.loadFromXMLElement(temp);
        }
        setAuthorizationInfo(authInfo);
    }

    public User() {
        name = "";
        registrationDate = null;
        info = "";
        authorizationInfo = new AuthorizationInfo();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getRegistrationDate() {
        return registrationDate;
    }

    public void setRegistrationDate(Date registrationDate) {
        this.registrationDate = registrationDate;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public AuthorizationInfo getAuthorizationInfo() {
        return authorizationInfo;
    }

    public void setAuthorizationInfo(AuthorizationInfo authorizationInfo) {
        this.authorizationInfo = authorizationInfo;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != User.class) {
            return false;
        }
        User other = (User) obj;
        return (id == other.id && name.equals(other.name)
                && registrationDate.equals(other.registrationDate)
                && authorizationInfo.equals(other.authorizationInfo)
                && info.equals(other.info));
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + this.id;
        hash = 67 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 67 * hash + (this.registrationDate != null ? this.registrationDate.hashCode() : 0);
        hash = 67 * hash + (this.info != null ? this.info.hashCode() : 0);
        hash = 67 * hash + (this.authorizationInfo != null ? this.authorizationInfo.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getName());
        sb.append(": [id=");
        sb.append(id);
        sb.append(", name=");
        sb.append(name);
        sb.append(", regDate=[");
        sb.append(registrationDate.toString());
        sb.append("], info=");
        sb.append(info);
        sb.append(", authInfo=[");
        sb.append(authorizationInfo.toString());
        sb.append("]]");
        return sb.toString();
    }

    public StringIntegerPair toStringIntegerPair() {
        return new StringIntegerPair(id, name);
    }
}
